package com.xialuo.study.agent;

import org.bsc.langgraph4j.GraphStateException;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.model.function.FunctionCallback;
import org.springframework.ai.tool.resolution.ToolCallbackResolver;

import java.util.List;

/**
 * 用于创建ReactAgent实例的构建器
 */
public class ReactAgentBuilder {
    private String name;
    private ChatClient chatClient;
    private String prompt;
    private List<FunctionCallback> tools;
    private ToolCallbackResolver resolver;
    private int maxIterations = 10;

    public ReactAgentBuilder name(String name) {
        this.name = name;
        return this;
    }

    public ReactAgentBuilder chatClient(ChatClient chatClient) {
        this.chatClient = chatClient;
        return this;
    }

    public ReactAgentBuilder prompt(String prompt) {
        this.prompt = prompt;
        return this;
    }

    public ReactAgentBuilder tools(List<FunctionCallback> tools) {
        this.tools = tools;
        return this;
    }

    public ReactAgentBuilder resolver(ToolCallbackResolver resolver) {
        this.resolver = resolver;
        return this;
    }

    public ReactAgentBuilder maxIterations(int maxIterations) {
        this.maxIterations = maxIterations;
        return this;
    }

    public ReactAgent build() throws GraphStateException {
        if (chatClient == null) {
            throw new IllegalArgumentException("必须提供ChatClient");
        }

        if (name == null) {
            name = "ReactAgent";
        }

        if (resolver != null) {
            return new ReactAgent(name, prompt, chatClient, resolver, maxIterations);
        } else if (tools != null) {
            return new ReactAgent(name, prompt, chatClient, tools, maxIterations);
        }

        throw new IllegalArgumentException("必须提供tools或resolver");
    }
}
